package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.db.domain.system.MpCashApply;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpCashApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户申请提现记录Controller
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Api(value = "用户申请提现记录",tags ="用户申请提现记录")
@RestController
@RequestMapping("/smp/cash/apply")
public class MpCashApplyController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpCashApplyController.class);
    @Autowired
    private IMpCashApplyService mpCashApplyService;

    /**
     * 查询用户申请提现记录列表
     */
    @Login
    @ApiOperation(value = "查询用户申请提现记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpCashApply",value="用户申请提现记录",paramType="form")
    })
    @GetMapping("/list")
    public TableDataInfo list(MpCashApply mpCashApply,@LoginUser MpClient client)
    {
        try {
            startPage();
            mpCashApply.setClientId(client.getClientId());
            List<MpCashApply> list = mpCashApplyService.selectMpCashApplyList(mpCashApply,client);
            return getDataTable(list);
        }catch (Exception e){
            log.error("用户申请提现记录发生异常",e);
        }
        return null;
    }

    /**
     * 获取用户申请提现记录详细信息
     */
    @ApiOperation(value = "获取用户申请提现记录详细信息")
    @ApiImplicitParam(name="cashApplyId",value="用户申请提现记录")
    @GetMapping(value = "/{cashApplyId}")
    public AjaxResult getInfo(@PathVariable("cashApplyId") Long cashApplyId)
    {
        try {
            return AjaxResult.success(mpCashApplyService.selectMpCashApplyByCashApplyId(cashApplyId));
        }catch (Exception e){
            log.error("用户申请提现记录发生异常",e);
        }
        return AjaxResult.error("用户申请提现记录异常");
    }

    /**
     * 新增用户申请提现记录
     */
    @Login
    @ApiOperation(value = "新增用户申请提现记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpCashApply",value="用户申请提现记录",dataType="MpCashApply")
    })
    @Log(title = "用户申请提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpCashApply mpCashApply, @LoginUser MpClient client)
    {
        try {
            if(client.getIsVirtual()!=null&&client.getIsVirtual()==1){
                return AjaxResult.success("虚拟号无法提现");
            }
            if(mpCashApply.getClientBankId()==null){
                return AjaxResult.error("请选择银行卡账户");
            }
            if(mpCashApply.getApplyAmount()==null){
                return AjaxResult.error("请输入提现金额");
            }
            if(mpCashApply.getApplyAmount().compareTo(new BigDecimal("10"))<0){
                return AjaxResult.error("提现金额不能低于10");
            }
            mpCashApplyService.insertMpCashApply(mpCashApply,client);
            return AjaxResult.success("提现成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 新增用户申请提现记录
     */
    @GetMapping("/getRate")
    public AjaxResult getRate()
    {
        Map<String,Object> map = mpCashApplyService.getMpCashApply();
        return AjaxResult.success(map);
    }


}
